package com.github.cosycode.imgtool.util;

import com.github.cosycode.common.util.otr.PrintTool;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Scanner;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2022/1/21
 * </p>
 *
 * @author CPF
 * @since 1.0
 **/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ScannerUtils {

    public static final Scanner SCANNER = new Scanner(System.in);

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String inputStringOrDefault(String title, String defaultValue) {
        PrintTool.warn("请输入 {}(default:{}):", title, defaultValue);
        String value;
        value = SCANNER.nextLine();
        if (StringUtils.isBlank(value)) {
            value = defaultValue;
        }
        PrintTool.success("    [{}]: {}", title, value);
        return value;
    }

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String inputActiveString(String title) {
        PrintTool.warn("请输入 {}", title);
        String value;
        while (true) {
            value = SCANNER.nextLine();
            if (StringUtils.isNotBlank(value)) {
                break;
            }
            PrintTool.warn("输入的值无效 ==> {}", value);
        }
        PrintTool.success("    [{}]: {}", title, value);
        return value;
    }

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static boolean confirm(String title) {
        PrintTool.warn("{} (yes/no):", title);
        while (true) {
            String value = SCANNER.nextLine().trim().toLowerCase();
            if (value.equals("y") || value.equals("yes")) {
                return true;
            }
            if (value.equals("n") || value.equals("no")) {
                return false;
            }
            PrintTool.warn("输入的值无效 ==> {}", value);
        }
    }

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static int select(String title, String[] option) {
        PrintTool.warn("{}:(请输入序号)", title);
        while (true) {
            for (int i = 1, len = option.length; i <= len; i++) {
                PrintTool.info("{}: {}", i, option[i]);
            }
            String value = SCANNER.nextLine().trim();
            try {
                int integer = Integer.parseInt(value);
                if (integer < 0 || integer > option.length) {
                    PrintTool.warn("输入的值需要在 [1, {}] 中 ==>", option.length);
                } else {
                    return integer;
                }
            } catch (NumberFormatException e) {
                PrintTool.warn("输入的值不是数字 ==> {}", value);
            }
        }
    }


    public static void main(String[] args) {
        final String rootPathDemo = "P:\\git-note\\cpfree";
        final String scanPathDemo = "P:\\git-note\\cpfree\\pblog\\design-code\\bit-data-map\\从云桌面获取数据.md";
        final String rootPath = inputStringOrDefault("项目所在根路径", rootPathDemo);
        final String scanPath = inputStringOrDefault("请输入markdown扫描文件夹或文件路径", scanPathDemo);
        boolean confirm = ScannerUtils.confirm("是否需要对匹配成功的图片进行移动");
    }

}
